package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.zip.Inflater;

@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @GetMapping("/list")
    public PageDTO<Item> page(@RequestParam Integer page, @RequestParam Integer size){
        Page<Item> itemPage = new Page<>(page,size);
        Page<Item> page1 = itemService.page(itemPage);
        return new PageDTO<>(itemPage.getTotal(),page1.getRecords());
    }
    @GetMapping("/{id}")
    public Item id(@PathVariable Long id){
        LambdaQueryWrapper<Item> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Item::getId,id);
        return itemService.getOne(queryWrapper);
    }
    @PostMapping
    public void save(@RequestBody Item item){
        itemService.save(item);
    }
    @PutMapping("/status/{id}/{status}")
    public void status(@PathVariable Long id,@PathVariable Integer status){
        UpdateWrapper<Item> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id).set("status",status);


        itemService.update(updateWrapper);
    }
    @PutMapping
    public void update(@RequestBody Item item){
        UpdateWrapper<Item> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",item.getId());
        itemService.update(item,updateWrapper);
    }
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id){
        itemService.removeById(id);
    }
}
